
var thirdAppCtrl  = new XFApp.client();
XFApp.client.prototype.thirdApp = {
	successCode : "10000",
	page : null,
	urlItems : {
		queryByPageURL : basePath+"/thirdApp/getPage",
		delURL : basePath+"/thirdApp/del",
		saveURL : basePath+"/thirdApp/save",
		getByIdURL : basePath+"/thirdApp/getById"
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			currentPageIndex : Number($("#currentPageIndex").val())-1,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(), 
		        success: function (data, pageIndex) {
		        	$("#currentPageIndex").val(data.result.pageNo);
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data 
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        	}else{
		        		window.webDialogUtil.alert("提示","操作失败");
		        	}
		        },
		        error : function(){
		        	window.webDialogUtil.alert("提示","操作失败");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	delDialog : function(id,appName){
		_this = this;
		webAlert({
            title:'提示',
            content:'确定要删除 '+appName+' 吗?',
            opacity:.5,
            bg:"gray",
            width:300,
            height:10,
            button:[
                    {
                    name:"确定",
                    callback:function(){
                    	_this.doDel(id);
                    	this.close();
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	doDel : function(id){
		_this = this;
		$.ajax({
		    url:_this.urlItems.delURL,
		    data:{id:id},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(true == data.isSuccess){
		    		thirdAppCtrl.thirdApp.queryPage();
		    	}
		     },  
		     error : function() {
		    	 window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	editDialog : function(id){
		_this = this;
		_this.cleanEditForm();
		if(null != id){
			$.ajax( {
			    url:_this.urlItems.getByIdURL,
			    data:{id:id},  
			    type:'post',  
			    cache:false,  
			    dataType:'json',  
			    success:function(data) {
			    	_this.fillEditForm(data.result);
			    	_this.openEditView(id);
			     },  
			     error : function() {  
			    	window.webDialogUtil.alert("提示","操作失败");
			     }  
			});
		}else{
			$("#editView #isNeedAuth").empty();
			$("#editView #isNeedAuth").append('<option value="1" selected="selected">需要授权</option>');
			$("#editView #isNeedAuth").append('<option value="0">不需要授权</option>');
			_this.openEditView();
		}
	},
	cleanEditForm : function(){
		$("#editForm")[0].reset();
	},
	fillEditForm : function(data){
		$("#editView #appName").val(data.appName);
		$("#editView #callBack").val(data.callBack);
		$("#editView #url").val(data.url);
		$("#editView #accessTimes").val(data.accessTimes);
		$("#editView #description").val(data.description);
		$("#editView #id").val(data.id);
		
		$("#editView #status").empty();
		if(1 == data.status){
			$("#editView #status").append('<option value="1" selected="selected">可使用</option>');
			$("#editView #status").append('<option value="0">已禁用</option>');
		}else{
			$("#editView #status").append('<option value="1">可使用</option>');
			$("#editView #status").append('<option value="0" selected="selected">已禁用</option>');
		}
		
	},
	openEditView : function(id){
		_this = this;
		webAlert({
            title:(null==id?"新增":"编辑"),
            content:document.getElementById("editView"),
            button:[
                    {
                    name:"保存",
                    callback:function(){
                    	_this.doSubmit(this);
                    	return false;
                    }
                    },{
                    name:"取消",
                    callback:function(){
                  	  this.close();
                    }
                    }]
            });
	},
	doSubmit : function(viewObj){
		_this = this;
		$.ajax( {
		    url:_this.urlItems.saveURL,
		    data:$('#editForm').serialize(),  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	window.webDialogUtil.alert("提示",data.msg);
		    	if(data.isSuccess){
		    		viewObj.close();
		    		_this.queryPage();
		    	}
		     },  
		     error : function() {  
		    	window.webDialogUtil.alert("提示","操作失败");
		     }  
		});
	},
	dataFormat : function(timeLong){
		if(null == timeLong
				|| "" == timeLong){
			return "";
		}
		return new Date(timeLong*1000).format("yyyy-MM-dd hh:mm:ss")
	},
	event : function(){
		_this = this;
		
		$("#searchButton").on("click",function(){
			_this.queryPage();
		});
		$("#addButton").on("click",function(){
			_this.editDialog();
		});
		
		thirdAppCtrl.thirdApp.queryPage();
	},
};
$.views.converters({
	ftime : function(value) {
		return thirdAppCtrl.thirdApp.dataFormat(value) ;
	},
	fstatus : function(value) {
		switch(value){
		case 1 : return "可使用";
		case 0 : return "禁止使用";
		default : return "未知状态";
		}
	},
	fgrade : function(value) {
		switch(value){
		case 1 : return "普通级别";
		default : return "未知级别";
		}
	}
});
$(function(){
	thirdAppCtrl.thirdApp.event();
});